/*****************************************************************************
 *   uarttest.c:  main C entry file for NXP LPC23xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.13  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "lpc24xx.h"                        /* LPC23xx/24xx definitions */
#include "type.h"
#include "irq.h"
#include "uart.h"
#include <stdio.h>

extern volatile DWORD UART0Count;
extern volatile BYTE UART0Buffer[BUFSIZE];
extern volatile DWORD UART1Count;
extern volatile BYTE UART1Buffer[BUFSIZE];
volatile BYTE Buffer[BUFSIZE];
int count;

/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
#define PLL_MValue			74
#define PLL_NValue			1
#define CCLKDivValue		5
int main (void)
{
						  DWORD MValue, NValue;

  if ( PLLSTAT & (1 << 25) )
  {
	PLLCON = 1;			/* Enable PLL, disconnected */
	PLLFEED = 0xaa;
	PLLFEED = 0x55;
  }

  PLLCON = 0;				/* Disable PLL, disconnected */
  PLLFEED = 0xaa;
  PLLFEED = 0x55;
    
//  SCS |= 0x20;			/* Enable main OSC */
//  while( !(SCS & 0x40) );	/* Wait until main OSC is usable */

  CLKSRCSEL = 0x0;		/* select main OSC, 12MHz, as the PLL clock source */

  PLLCFG = PLL_MValue | (PLL_NValue << 16);
  PLLFEED = 0xaa;
  PLLFEED = 0x55;
      
  PLLCON = 1;				/* Enable PLL, disconnected */
  PLLFEED = 0xaa;
  PLLFEED = 0x55;

  CCLKCFG = CCLKDivValue;	/* Set clock divider */


  while ( ((PLLSTAT & (1 << 26)) == 0) );	/* Check lock bit status */
    
  MValue = PLLSTAT & 0x00007FFF;
  NValue = (PLLSTAT & 0x00FF0000) >> 16;
  while ((MValue != PLL_MValue) && ( NValue != PLL_NValue) );

  PLLCON = 3;				/* enable and connect */
  PLLFEED = 0xaa;
  PLLFEED = 0x55;
  while ( ((PLLSTAT & (1 << 25)) == 0) );	/* Check connect bit status */





 // UARTInit(0, 9600);	/* baud rate setting */
	PINSEL0 = 0x00000050;
	PINSEL4 |=  0x000A0000;       /* RxD0 and TxD0 */
	//PINMODE0= 0x000000A0;

    U0LCR = 0x83;		/* 8 bits, no Parity, 1 Stop bit */
 
 
    U0DLM = 0x0;							
    U0DLL = 0x2E;
	U0FDR = 0xda;
	U0LCR = 0x03;		/* DLAB = 0 */

    U0FCR = 0x07;		/* Enable and reset TX and RX FIFO. */


  IODIR1=0x00000020;


  	U2LCR = 0x83;
	U2DLM = 0x0;							
    U2DLL = 0x2E;
	U2FDR = 0xda;
	U2LCR = 0x03;		/* DLAB = 0 */

	U2FCR = 0x07;


	PINSEL0 |= 0x40000000;	/* Enable TxD1 P0.15 */
	PINSEL1 |= 0x00000001;	/* Enable RxD1 P0.16 */

	U1LCR = 0x83;
	U1DLM = 0x0;							
    U1DLL = 0x2E;
	U1FDR = 0xda;
	U1LCR = 0x03;		/* DLAB = 0 */

	U1FCR = 0x07;
//	while(1){
		//
		U2THR = 'i';
		U0THR = 'H';
		
		U1THR = 'H';
		U1THR = 'E';
		U1THR = 'L';
		U1THR = 'L';
		U1THR = 'O';
		
		U1THR = ' ';
		U1THR = 'W';
		U1THR = 'O';
		U1THR = 'R';
		U1THR = 'L';
		U1THR = 'D';
		U1THR = '!';

//	}
	
	while(1);



 	 
  return 0;
  
}

/*****************************************************************************
**                            End Of File
*****************************************************************************/
